const app = getApp();
Page({
  data: {
    labels: [
    ],
  },
  onLoad: function () {
    this.initPage();
  },
  initPage: function () {
    var _this = this;
    _this.data.labelChecked = [];//初始化标签
    app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (v) {
      var userInfo = wx.getStorageSync('userInfo');
      app.doReq(app.globalData.DICTIONARY, {
        parentId: 929
      }, 'get', function (res) {
        var data = res.data;
        var labelListTemp = [];
        var details = userInfo.detail;
        var label = details.label//当前用户的个人标签
        if (data.code == 200) {
          var list = data.obj[929];
          for (let i in list) {
            let obj = {};
            obj.name = list[i].name;
            obj.value = list[i].id;
            if (label.indexOf(list[i].id) >= 0) {
              obj.checked = true;
              _this.data.labelChecked.push(obj.value)
            } else {
              obj.checked = false;
            }
            labelListTemp.push(obj);
          }
          _this.setData({
            labels: labelListTemp
          });
        }
      });
    });
  },
  checkboxChange: function (e) {
    var labels = this.data.labels;//全部标签
    for (let x of labels) {
      if (x.value == e.currentTarget.dataset.value) {
        if (x.checked) {
          x.checked = false
        } else {
          x.checked = true
        }
      }
    }
    this.setData({
      labels: labels
    });
  },
  doCancel: function () {
    wx.navigateBack({})
  },
  doSave: function () {
      let _this = this;
      var labelId=[];
      for (let i of _this.data.labels){
        if(i.checked){
          labelId.push(i.value);
        }
      }
      app.doReq(app.globalData.SERVICE.USERCENTER.TAG_SAVE,{
        'xgdetail.label':labelId.toString(),
        'id':app.globalData.userid
      },'get',function(res){
          var data=res.data
          if(data.code==200 && data.type=='SUCCESS'){
            wx.showToast({
              title: '保存成功！',
              icon:'success'
            });
            app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (value) {
              var pages = getCurrentPages();
              var currentPage = pages[pages.length - 1];//当前页面
              var pervPagge = pages[pages.length - 2];//上个页面
              pervPagge.setData({
                reloadFlag: true,
              })
              wx.navigateBack({});
            });
          }
      })
  }
})
